/**
 * @desc 撤销重做
 * @author: FFFF
 */
import { ref } from 'vue'
import { getNowDateTime } from './util.js'
function Revoke() {

    // 记录列表，用于撤销
    let recordList = ref([]);

    // 撤销记录，用于重做
    let undoList = ref([]);

    // 当前记录用currentRecord变量暂时存储，当用户修改时，再存放到recordList
    let currentRecord = ref(null);

    // 历史记录
    let historyList = ref([]);

    // 最后记录时间
    let lastPushTime = 0;

    /**
     * @description: 插入历史记录
     * @param {object}record
     * @return {boolean}
     */
    function add(record, type) {

        if (type === void 0) { type = '插入组件'; }

        // 忽略低于150ms时间差的记录
        var nowTime = Date.now();
        if (lastPushTime + 150 > nowTime) {
            return;
        }
        lastPushTime = nowTime;

        // 判断之前是否已经存在currentRecord记录，有则存储到recordList
        if (currentRecord.value != null) {
            recordList.value.push(currentRecord.value);
            // 增加记录后则应该清空重做记录
            undoList.value.splice(0, undoList.value.length);
        }

        // 将json转成字符串存储
        currentRecord.value = {
            type: type,
            record: JSON.stringify(record)
        };

        this.addHistory(record, type)

        // 最多存储20条记录，超过20条记录则删除之前的记录
        if (recordList.value.length > 20) {
            recordList.value.unshift();
        }
    }

    /**
     * @description: 添加历史记录
     * @param {object}record
     * @return {*}
     */
    function addHistory(record, type) {
        // 添加到历史记录
        historyList.value.push({
            type: type,
            record: record,
            time: getNowDateTime()
        });
    }

    /**
     * @description: 清空历史记录
     * @param {*}
     * @return {*}
     */
    function clearHistory(callback) {
        historyList.value = [];
        if (typeof callback === 'function') callback();
    }


    /**
     * @description: 撤销操作
     * @param {*}
     * @return {object}
     */
    function undo() {

        // 没有记录时,返回false
        if (recordList.value.length === 0) {
            return false;
        }

        var recordObj = recordList.value.pop();

        // 将当前记录添加到重做记录里面
        if (currentRecord.value != null) {
            undoList.value.push(currentRecord.value);
        }

        // 丢弃当前记录，防止重复添加
        currentRecord.value = recordObj;
        this.addHistory(recordObj, "撤销操作");
        return JSON.parse(recordObj.record);
    }

    /**
     * @description: 重做操作
     * @param {*}
     * @return {*}
     */
    function redo() {

        // 没有重做记录时,返回false
        if (undoList.value.length === 0) {
            return false;
        }

        var recordObj = undoList.value.pop();

        // 添加到重做记录里面
        if (currentRecord.value != null) {
            recordList.value.push(currentRecord.value);
        }

        // 丢弃当前记录，防止重复添加
        currentRecord.value = recordObj;

        this.addHistory(recordObj, "重做操作");

        return JSON.parse(recordObj.record);

    }
    return {
        recordList: recordList,
        undoList: undoList,
        currentRecord: currentRecord,
        historyList: historyList,
        addHistory: addHistory,
        clearHistory: clearHistory,
        add: add,
        undo: undo,
        redo: redo
    };
}

export const revoke = Revoke()